package com.Logistics.pdusers.controller;


import com.Logistics.mycommon.pojo.pdusers.PdAddressBook;
import com.Logistics.pdusers.service.PdAddressBookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 地址簿 前端控制器
 * </p>
 *
 * @author PinDaLogistics
 * @since 2024-08-05
 */
@CrossOrigin
@RestController
@RequestMapping("/serviceusers/pd-address-book")
public class PdAddressBookController {

    @Autowired
    private PdAddressBookService pdAddressBookService;

    @PostMapping
    public Boolean insertAddress(@RequestBody PdAddressBook pdAddressBook){
        pdAddressBook.setUserId(pdAddressBook.getUserId());
        pdAddressBook.setProvinceId(pdAddressBook.getProvinceId());
        pdAddressBook.setName(pdAddressBook.getName());
        pdAddressBook.setCityId(pdAddressBook.getCityId());
        pdAddressBook.setCountyId(pdAddressBook.getCountyId());
        pdAddressBook.setAddress(pdAddressBook.getAddress());
        pdAddressBook.setCreateTime(new Date());
        pdAddressBook.setIsDefault(false);
        return pdAddressBookService.save(pdAddressBook);
    }

    @GetMapping("/{userId}")
    public List<PdAddressBook> addressbooksByUserId(@PathVariable Integer userId){
        return pdAddressBookService.addressByUserId(userId);
    }

    //根据地址的主键删除地址
    @DeleteMapping("/{id}")
    public Boolean deleteAddressById(@PathVariable Integer id){
        return pdAddressBookService.removeById(id);
    }


    @GetMapping("/detail/{id}")
    PdAddressBook detail(@PathVariable Integer id){
        return pdAddressBookService.getById(id);
    };

    @PostMapping("/select")
    public List<PdAddressBook> selectbyAddressName(@RequestParam String name){
        return pdAddressBookService.selectbyAddressName(name);
    }
}

